package net.zoneland.knowledge.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import net.zoneland.knowledge.constant.Constants;
import net.zoneland.knowledge.model.*;
import net.zoneland.knowledge.utils.QaBankUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import net.zoneland.knowledge.constant.FieldConstant;
import net.zoneland.knowledge.dao.QaBankMapper;
import net.zoneland.knowledge.enums.VisitTypeEnum;
import net.zoneland.knowledge.service.QaBankService;
import net.zoneland.knowledge.utils.GsonUtils;

/**
 * .
 *
 * @author: bcc
 * 2021/6/9
 */
@Service
public class QaBankServiceImpl implements QaBankService {

    @Autowired
    private QaBankMapper qaBankMapper;

    @Override
    public List<QaBankWithBLOBs> queryByPiidAndBankId(final String piid, final String bankId) {
        final QaBankExample example = new QaBankExample();
        example.createCriteria().andIdEqualTo(bankId);
        return qaBankMapper.selectByExampleWithBLOBs(example);
    }

    @Override
    public void loadMoreInfoFromDb(final EsQuestion esQuestion, final VisitTypeEnum visitTypeEnum) {
        QaBankWithBLOBs qaBank = null;
        if (esQuestion.isLoadFromDb()) {
            qaBank = loadAttachmentFromDb(esQuestion);
        }
        
        if (null != esQuestion.getShowRichText() && esQuestion.getShowRichText()) {
            loadRichText(esQuestion, qaBank, visitTypeEnum);
        }
    }

    private QaBankWithBLOBs loadRichText(final EsQuestion esQuestion, QaBankWithBLOBs qaBank, final VisitTypeEnum visitTypeEnum) {
        if (null == qaBank) {
            qaBank = qaBankMapper.selectByPrimaryKey(esQuestion.getId());
        }
        if (qaBank != null ) {
            if (visitTypeEnum == VisitTypeEnum.MOA) {
                esQuestion.setAnswer(qaBank.getMoaRichText());
            }else {
                esQuestion.setAnswer(qaBank.getPcRichText());
            }
        }
        return qaBank;
    }

    private QaBankWithBLOBs loadAttachmentFromDb(final EsQuestion esQuestion) {

        QaBankWithBLOBs qaBank = qaBankMapper.selectByPrimaryKey(esQuestion.getId());
        if (qaBank == null || StringUtils.isBlank(qaBank.getAnsContentName())) {
            // 20211120 兼容loadFromDb设置错误时，触发的空指针异常
            esQuestion.setLoadFromDb(false);
            return qaBank;
        }
        List<TalkResAnswersFile> ansContentImageList = new ArrayList<>();
        Map<String, Map<String, String>> ansContentName = GsonUtils.GSON.fromJson(qaBank.getAnsContentName(), Map.class);
        Map<String, String> ansContentNameImageMap = ansContentName.get("image");
        //处理图片信息
        if (ansContentNameImageMap != null) {
            for (String unid : ansContentNameImageMap.keySet()) {
                TalkResAnswersFile talkResAnswersFile = new TalkResAnswersFile();
                talkResAnswersFile.setFileId(unid);
                talkResAnswersFile.setPiid(null);
                talkResAnswersFile.setQaBankId(qaBank.getId());
                talkResAnswersFile.setFileName(ansContentNameImageMap.get(unid));
                ansContentImageList.add(talkResAnswersFile);
            }
        }

        Map<String, String> ansContentNameFileMap = ansContentName.get("file");
        List<TalkResAnswersFile> ansContentFileList = new ArrayList<>();
        //处理文件信息
        if (ansContentNameFileMap != null) {
            for (String unid : ansContentNameFileMap.keySet()) {
                TalkResAnswersFile talkResAnswersFile = new TalkResAnswersFile();
                talkResAnswersFile.setFileId(unid);
                talkResAnswersFile.setPiid(null);
                talkResAnswersFile.setQaBankId(qaBank.getId());
                talkResAnswersFile.setFileName(ansContentNameFileMap.get(unid));
                ansContentFileList.add(talkResAnswersFile);
            }
        }
        if (CollectionUtils.isNotEmpty(ansContentFileList) || CollectionUtils.isNotEmpty(ansContentImageList)) {
            Map<String, Object> data = new HashMap<>();
            if (CollectionUtils.isNotEmpty(ansContentFileList)) {
                data.put(FieldConstant.FIELD_ANS_CONTENT_FILE_LIST, ansContentFileList);
            }
            if (CollectionUtils.isNotEmpty(ansContentImageList)) {
                data.put(FieldConstant.FIELD_ANS_CONTENT_IMAGE_LIST, ansContentImageList);
            }
            esQuestion.setDataFormDb(data);
        }
        return qaBank;
    }

    /**
     * 通过科目查找问题.
     *
     * @param qaBank
     * @return
     */
    @Override
    public List<QaBankWithBLOBs> queryBySubject(final QaBank qaBank, final LoginUserPojo loginUserPojo,
                                                final List<String> currentUserRole) {
        return qaBankMapper.selectBySubject(qaBank, loginUserPojo.getUserID()).stream()
                .filter(blobs -> QaBankUtils.checkQaBankToFilter(blobs.getUserids(),
                        blobs.getDeptids(), blobs.getCategory(), blobs.getLevel1Title(), loginUserPojo))
                .collect(Collectors.toList());
    }

}
